#include <assert.h>
#include "fx.h"
#include <stdlib.h>
#include "animation.h"
#include "point2.h"
#include "quad.h"
#include "frame.h"
#include "rect.h"

Fx::Fx(){
	animation =NULL;
	position =NULL;
	dirrection =NULL;
	quad =NULL;
}

Fx::~Fx(){
	delete animation;
	animation =NULL;
	delete position;
	position =NULL;
	delete dirrection;
	dirrection =NULL;

	if(quad){
		for(int i=0; i<4; i++){
			delete quad->v[i];
			quad->v[i] =NULL;
		}
	}
	delete quad;
	quad =NULL;
}

Fx::Fx( const Fx &in )
{
	layer =in.layer;
	animation =new Animation(*in.animation);
//		animation =in.animation;	//?
	repeat =in.repeat;
	position =NULL;
//	position =new Point2;
//	*position =*in.position;
	dirrection =NULL;
//	dirrection =new Point2;
//	*dirrection =*in.dirrection;
	quad =NULL;
	type =in.type;
//	quad =new Quad<Point2*>;//(in.quad->v[0], in.quad->v[1], in.quad->v[2], in.quad->v[3]);	//?
//	quad->v[0] =new Point2(*in.quad->v[0]);
//	quad->v[1] =new Point2(*in.quad->v[1]);
//	quad->v[2] =new Point2(*in.quad->v[2]);
//	quad->v[3] =new Point2(*in.quad->v[3]);

//	width =in.width;
//	length =in.length;
}

Quad<Point2*>* Fx::getQuad(Point2* position, Point2* dirrection){

	assert(position && dirrection);
	float width, length;	

	width =animation->frames[animation->cur_frame]->body->xHi -animation->frames[animation->cur_frame]->body->xLo;
	length =animation->frames[animation->cur_frame]->body->yHi -animation->frames[animation->cur_frame]->body->yLo;

	Quad<Point2*>* quad;
	Point2 normal;

	quad =new Quad<Point2*>;

	normal.x =-dirrection->y;
	normal.y =dirrection->x;
	dirrection->Normalize();
	normal.Normalize();

	quad->v[0] =new Point2;
	*(quad->v[0]) =*position +normal*(width/2);	//? opt

	quad->v[1] =new Point2;
	*(quad->v[1]) =*position +normal*(-width/2);	//? opt

	quad->v[2] =new Point2;
	*(quad->v[2]) =*(quad->v[1]) +*dirrection*length;

	quad->v[3] =new Point2;
	*(quad->v[3]) =*(quad->v[0]) +*dirrection*length;

	return quad;
}
/*
Quad<Point2*>* Quad::createQuad(Point2* dirrection, float width, float length){

	Quad<Point2*>* quad;
	Point2 normal;

	quad =new Quad<Point2*>;

	normal.x =-dirrection.y;
	normal.y =dirrection.x;
	dirrection.Normalize();
	normal.Normalize();

	quad->v[0] =new Point2;
	*(quad->v[0]) =position +normal*width;

	quad->v[1] =new Point2;
	*(quad->v[1]) =o +ray*10 +normal*(-width);

	quad->v[2] =new Point2;
	*(quad->v[2]) =*(quad->v[1]) +dirrection*length;

	quad->v[3] =new Point2;
	*(quad->v[3]) =*(quad->v[0]) +dirrection*length;

	return quad;
}
*/